import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:get/get.dart';
import 'package:smart_translator/app/utils/CommonUtil.dart';
import '../../db/DbDAOHelper.dart';
import '../../db/database.dart';
import '../../style/app_colors.dart';
import '../../style/widget_style.dart';
import '../../utils/tips.dart';
import '../../widgets/AudioWaveform.dart';
import '../../widgets/CommonWidgets.dart';
import '../controller/RecordTranscriptViewRecordingController.dart';

class Record2scriptRecord2text extends GetView<RecordTranscriptViewRecordingController> {
  const Record2scriptRecord2text({super.key});
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: AppColors.scaffold_background_color,
      body: Obx(()=>Column(children: [
        Expanded(child: controller.hasTranScript.value? controller.audioType == 'new'? Container(
          child: Column(
            children: [
              Expanded(child: Container(
                margin: EdgeInsets.all(18.w),
                child: ListView.builder(itemBuilder: (context,index) {
                  if (index == controller.audioTranscriptSource.length) { // 把list 大小增加一个,承载临时会话内容.
                    return Column(
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        Container(
                          padding: EdgeInsets.all(18.w),
                          decoration: BoxDecoration(
                              shape: BoxShape.rectangle,
                              borderRadius: BorderRadius.all(Radius.circular(20)),
                              color: Colors.white
                          ),
                          child: Obx(()=>Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Row(
                                children: [
                                  Text('${CommonUtil.getMMSSFormate(controller.globalDuration.value)}',style: AppStyleThemeData.HINT_TEXT_STYLE_small),
                                  SizedBox(width: 23.w,),
                                  Icon(Icons.person,color: Colors.grey),
                                  Text('${controller.currentSpeaker.value == 'Unknown' ? '未知': controller.currentSpeaker.value}',style: AppStyleThemeData.HINT_TEXT_STYLE_small,)
                                ],
                              ),
                              SizedBox(height: 10.w,),
                              Text('${controller.finalFromLanguage.value}',style: AppStyleThemeData.normal,)
                            ],
                          )),
                        ),
                        SizedBox(height: 16.h,),

                      ],
                    );
                  } else {
                    AudioTranscriptData audio = controller.audioTranscriptSource.value[index];
                    if (audio != null) {
                      return Column(
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          Container(
                            padding: EdgeInsets.all(18.w),
                            decoration: BoxDecoration(
                                shape: BoxShape.rectangle,
                                borderRadius: BorderRadius.all(Radius.circular(20)),
                                color: Colors.white
                            ),
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Row(
                                  children: [
                                    Text('${CommonUtil.getMMSSFormate(audio.duration)}',style: AppStyleThemeData.HINT_TEXT_STYLE_small),
                                    SizedBox(width: 23.w,),
                                    Icon(Icons.person,color: Colors.grey,),
                                    Text('${audio.speaker == 'Unknown' ? '未知': audio.speaker}',style: AppStyleThemeData.HINT_TEXT_STYLE_small,)
                                  ],
                                ),
                                SizedBox(height: 10.w,),
                                Text('${audio.a_text}',style: AppStyleThemeData.normal,)
                              ],
                            ),
                          ),
                          SizedBox(height: 16.h,),

                        ],
                      );
                    }

                  }
                },
                    itemCount: controller.audioTranscriptSource.length + 1,
                    controller:controller.chatListScrollController),
              ),flex: 1,),

              Container(
                color: Colors.white.withValues(alpha: 0.7),
                margin: EdgeInsets.only(bottom: 20.h),
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    controller.audioService.isPlaying.value ? Row(
                      children: [
                        Expanded(child:AudioWaveform(
                          amplitudes: controller.amplitudes.value,
                          height: 20.h,
                          width: 200.w,
                          waveColor: controller.audioService.isPlaying.value ? Colors.blue : Colors.grey,
                        ),flex: 1,),
                        Expanded(child:AudioWaveform(
                          amplitudes: controller.amplitudes.value,
                          height: 20.h,
                          width: 200.w,
                          waveColor: controller.audioService.isPlaying.value ? Colors.blue : Colors.grey,
                        ),flex: 1,),
                      ],
                    ) : Row(
                      children: [
                        Expanded(child: Image.asset('assets/images/icon_audio_wave_realtime.png',height: 25.h,),flex: 1,),
                        Expanded(child: Image.asset('assets/images/icon_audio_wave_realtime.png',height: 25.h,),flex: 1,)
                      ],
                    ),
                    SizedBox(height: 14.h,),
                    Row(
                      children: [
                        Expanded(child: Container(
                          child: Column(
                            children: [
                              InkWell(
                                onTap: () {
                                  if (controller.audioService.recordingSecond != 0) {
                                    DbDAOHelper().updateSessionLengthyById(int.parse(controller.sessionId!),
                                        controller.audioService.recordingSecond);
                                  }
                                  controller.stopSimulateAudioData();
                                  controller.audioStop = true;
                                  controller.audioService.stopRecorder();
                                  Get.back();

                                },
                                child: Container(
                                  width: 40.w,
                                  height: 40.h,
                                  decoration: BoxDecoration(
                                      shape: BoxShape.circle,
                                      color: Colors.red
                                  ),
                                  child: SvgPicture.asset('assets/images/icon_transcript_termiate_audio.svg'),
                                ),
                              ),
                              SizedBox(height: 10.h,),
                              Text('结束录音',style: AppStyleThemeData.HINT_TEXT_STYLE_small,)
                            ],
                          ),
                        ),flex: 1,),
                        Expanded(child: Container(
                          child: Column(
                            children: [
                              InkWell(
                                onTap: () {
                                  if (controller.audioStop) {
                                    Tips.showToast('录音已经结束.');
                                    return;
                                  }

                                  if (controller.audioService.isPlaying.value) {
                                    controller.stopSimulateAudioData();
                                    controller.audioService.pauseRecorder();
                                  } else {
                                    controller.simulateAudioData();
                                    controller.audioService.record();

                                  }

                                  controller.hasTranScript.value = true;
                                },
                                child: controller.audioService.isPlaying.value ? Image.asset('assets/images/icon_play_realtime_btn_running.png',width: 55.w,height: 55.h,):
                                Image.asset('assets/images/icon_record_transcript_pause.png',width: 55.w,height: 55.h,),
                              ),
                              Text('${controller.audioService.recordShowSeconds.value}',style: TextStyle(fontSize: 14.sp,fontWeight: FontWeight.w600),)

                            ],
                          ),
                        ),flex: 1,),
                        Expanded(
                          child: Row(
                            children: [
                              Expanded(child: InkWell(
                                onTap: () {
                                    controller.chooseTranscriptLanguages();
                                },
                                child:  Container(
                                  child: Column(
                                    children: [
                                      Text('${controller.audioService.fromLanFlagDisplay.value.substring(0,1)}',style: TextStyle(
                                          color: AppColors.listener_bottom_label,
                                          fontSize: 16.sp,
                                          fontWeight: FontWeight.bold
                                      ),),
                                      SizedBox(height: 10.h,),
                                      Text('语言',style: AppStyleThemeData.HINT_TEXT_STYLE_small,)
                                    ],
                                  ),
                                ),
                              ),flex: 1,),
                              Expanded(child: Container(
                                child: Column(
                                  children: [
                                    Image.asset('assets/images/icon_record_script_bottom_flag.png',width: 24.w,height: 24.h),
                                    SizedBox(height: 10.h,),
                                    Text('标记',style: AppStyleThemeData.HINT_TEXT_STYLE_small,)
                                  ],
                                ),
                              ),flex: 1,)

                            ],
                          ),
                          flex: 1,
                        )

                      ],
                    )
                  ],
                ),
              )

            ],
          ),

        ): Stack(
          children: [
            Container(
              margin: EdgeInsets.all(18.w),
              child: ListView.builder(itemBuilder: (context, index) {
                AudioTranscriptDataExtends audio = controller.audioTranscriptSource.value[index];
                if (audio != null) {
                  return GestureDetector(
                    onLongPress: () {
                      // print('long press...');
                      controller.changeEditMode(audio);

                    },
                    child: Column(
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        Container(
                          padding: EdgeInsets.all(18.w),
                          decoration: BoxDecoration(
                              shape: BoxShape.rectangle,
                              borderRadius: BorderRadius.all(Radius.circular(20)),
                              color: Colors.white
                          ),
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Row(
                                mainAxisAlignment: MainAxisAlignment.spaceBetween,children: [
                                  Row(
                                    children: [
                                      Text('${CommonUtil.getMMSSFormate(audio.duration)}',style: AppStyleThemeData.HINT_TEXT_STYLE_small),
                                      SizedBox(width: 23.w,),
                                      Icon(Icons.person,color: Colors.grey,),
                                      Text('${audio.speaker}',style: AppStyleThemeData.HINT_TEXT_STYLE_small,)
                                    ],
                                  ),
                                 (controller.script_edit_mode.value && audio.isChecked) ? Checkbox(value: audio.isChecked, onChanged: (v) {
                                       print('vvvvv-----> ${v}');
                                       // controller.scriptItemChecked(audio,v!);
                                    },
                                    shape: CircleBorder(), // 关键：设置为圆形边框
                                    activeColor: Colors.blue, // 选中时的颜色
                                    checkColor: Colors.white,

                                  ) : SizedBox()
                                ],
                              ),
                              SizedBox(height: 10.w,),
                              (controller.script_edit_mode.value && audio.isChecked)? Container(
                                width: Get.width,
                                child: TextField(
                                  controller: controller.scriptController,
                                  maxLines: null,
                                  decoration: InputDecoration(
                                    contentPadding: EdgeInsets.all(5), // 完全去掉内边距
                                    focusedBorder: OutlineInputBorder(
                                      borderSide: BorderSide(color: Colors.blue, width: 1.0),
                                      borderRadius: BorderRadius.circular(8.0),
                                    ),
                                    /* border: OutlineInputBorder(
                                      borderSide: BorderSide(color: Colors.blue, width: 1.0),
                                    ), // 可选：边框样式*/
                                  ),
                                  onChanged: (v) {
                                    controller.executeSaveScript();
                                  },
                                ),
                              ):
                              Text('${audio.a_text}',style: AppStyleThemeData.normal,)
                            ],
                          ),
                        ),
                        SizedBox(height: 16.h,),

                      ],
                    ),
                  );
                }

              },
                itemCount: controller.audioTranscriptSource.length,
              ),
            ),
            controller.script_edit_mode.value ? Positioned(child: Container(
              width: Get.width,
              padding: EdgeInsets.only(top: 10.h,bottom: 20.h),
              decoration: BoxDecoration(
                boxShadow: [
                  CommonWidgets.getBoxShadow(),
                ],
                color:  Colors.white,
              ),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceAround,
                children: [
                  InkWell(
                    onTap: () {
                      controller.executeDeleteScript();
                    },
                    child: Container(
                      child: Column(
                        children: [
                          Image.asset('assets/images/icon_script_bottom_menu_delete.png',width: 24.w,height: 24.h,),
                          SizedBox(height: 6.h),
                          Text('删除',style: TextStyle(color: Colors.red),)
                        ],
                      ),
                    ),
                  ),
                  InkWell(
                    onTap: () {
                      controller.executeCopyScript();
                    },
                    child: Container(
                      child: Column(
                        children: [
                          Image.asset('assets/images/icon_script_bottom_menu_copy.png',width: 24.w,height: 24.h,),
                          SizedBox(height: 6.h),
                          Text('复制')
                        ],
                      ),
                    ),
                  ),
                  InkWell(
                    onTap: () {
                      controller.executeEditScriptList();
                    },
                    child: Container(
                      child: Column(
                        children: [
                          Image.asset('assets/images/icon_script_bottom_menu_editor.png',width: 24.w,height: 24.h,),
                          SizedBox(height: 6.h),
                          Text('编辑')
                        ],
                      ),
                    ),
                  ),
                 /* Container(
                    child: Column(
                      children: [
                        Image.asset('assets/images/icon_script_bottom_menu_tag.png',width: 24.w,height: 24.h,),
                        SizedBox(height: 6.h),
                        Text('标记')
                      ],
                    ),
                  ),*/
                  /*InkWell(
                    onTap: () {
                      controller.executeSaveScript();
                    },
                    child: Container(
                      child: Column(
                        children: [
                          Image.asset('assets/images/icon_script_bottom_menu_save.png',width: 24.w,height: 24.h,),
                          SizedBox(height: 6.h),
                          Text('保存')
                        ],
                      ),
                    ),
                  ),*/
                ],
              ),
            ),bottom: 0.h,) : SizedBox()
          ],
        ): controller.audioType == 'new' ? Center(
          child: Column(
            children: [
              SizedBox(height: 113.h,),
              Image.asset('assets/images/icon_commeeting.png',width: 251.w,height: 165.h),
              SizedBox(height: 16.h,),
              Container(
                child: Text('录音过程,请选择噪音比较小的环境并保持发音标准,音量清晰',style: AppStyleThemeData.HINT_TEXT_STYLE_small,),
              )

            ],
          ),
        ): Center(
          child: Column(
            children: [
              SizedBox(height: 80.h,),
              Image.asset('assets/images/empty_list_realtime.png',width: 169.w,height: 91.h),
              SizedBox(height: 16.h,),
              Text('暂无转写记录',style: AppStyleThemeData.HINT_TEXT_STYLE_small,)
            ],
          ),
        ) ,flex: 1,),
        //assets/images/icon_audio_wave_realtime.png
      ]
      ))
    );
  }
}
